package cn.coco.jf.fw.admin.model;

import java.math.BigDecimal;
import java.util.List;

import cn.exlu.coco.core.exception.CoCoRuntimeException;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Table;
import com.jfinal.plugin.activerecord.TableMapping;

@Deprecated
public class OracleBaseModel<M extends Model> extends Model<M>{
	
	public List<M> findAllLoadColumns(String columns){
		Table table = TableMapping.me().getTable(getUsefulClass());
		StringBuilder sb = new StringBuilder();
		sb.append("select " + columns).append(" from " + table.getName());
		
		return this.find(sb.toString());
	}
	
	private Class<? extends Model> getUsefulClass() {
		Class c = getClass();
		return c.getName().indexOf("EnhancerByCGLIB") == -1 ? c : c.getSuperclass();	// com.demo.blog.Blog$$EnhancerByCGLIB$$69a17158
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public Integer getInt(String attr){
		Object obj = get(attr);
		
		if(obj == null){
			return null;
		}
		
		if(obj instanceof Integer){
			return (Integer)obj;
		}else if(obj instanceof BigDecimal){
			return ((BigDecimal)obj).intValue();
		}
		
		throw new CoCoRuntimeException("数据类型不匹配, 属性名："+attr);

	}
	
	public Long getLong(String attr){
		BigDecimal bd = get(attr);
		if(bd == null){
			return null;
		}
		
		return bd.longValue();
	}
	
	/**
	 * Get attribute of mysql type: varchar, char, enum, set, text, tinytext, mediumtext, longtext
	 */
	public String getStr(String attr) {
		Object obj = get(attr);
		if(obj instanceof String){
			return get(attr);
		}
		
		if(obj instanceof BigDecimal){
			return obj.toString();
		}
		
		return get(attr);
	}
}
